<?php
//微信H5支付,扫码支付
class WeixinPay{
	/**
     * H5支付发起订单
     * @$appid 公众账号ID
	 * @$mch_id 商户号	
	 * @$body 商品描述
	 * @$out_trade_no 商户订单号
	 * @$money 金额
	 * @$notify_url 异步地址
	 * @$wap_url WAP网站URL地址 www.baidu.com
	 * @$wap_name WAP网站名
	 * @$apiKey API密钥
    */   
	public static function Pay_h5($appid,$mch_id,$body,$out_trade_no,$money,$notify_url,$wap_url,$wap_name,$apiKey){        
        $array = array(
            'appid' =>$appid,
			'mch_id' =>$mch_id,
			'nonce_str' =>self::createNonceStr(),
			'body' =>$body,
			'attach' => 'pay',
			'out_trade_no' =>$out_trade_no,
			'total_fee' =>intval($money * 100),
			'spbill_create_ip' =>$_SERVER['REMOTE_ADDR'],
			'notify_url' =>$notify_url,
			'trade_type' =>'MWEB',
			'scene_info' =>json_encode(array('h5_info' =>array('type'=>'Wap','wap_url'=>$wap_url,'wap_name'=>$wap_name)))
		);
        $array['sign'] = self::getSign($array,$apiKey);
        $arrayToXml = self::curlPost('https://api.mch.weixin.qq.com/pay/unifiedorder',self::arrayToXml($array));
        $simplexml = simplexml_load_string($arrayToXml, 'SimpleXMLElement', LIBXML_NOCDATA);
		$data = json_decode(json_encode($simplexml),true);
		switch ($data['return_code']){
			case "SUCCESS": 
			    $param = array('code'=>200,'url' =>$data['mweb_url']);
			break;			
			default: $param = array('code'=>201,'msg' =>$data['return_msg']);
		}		
		return $param;
    }
	
	/**
     * 扫码支付发起订单
     * @$appid 公众账号ID
	 * @$mch_id 商户号	
	 * @$body 商品描述
	 * @$orderid 商户订单号
	 * @$money 金额
	 * @$notify_url 异步地址
	 * @$apiKey API密钥
    */
	public static function Pay_Scan($appid,$mch_id,$body,$orderid,$money,$notify_url,$apiKey){        
        $array = array(
            'appid' =>$appid,
			'mch_id' =>$mch_id,
			'nonce_str' =>self::createNonceStr(),
			'body' =>$body,
			'attach' => 'pay',
			'out_trade_no' =>$orderid,
			'total_fee' =>intval($money * 100),
			'spbill_create_ip' =>$_SERVER['REMOTE_ADDR'],
			'notify_url' =>$notify_url,
			'trade_type' =>'NATIVE'
		);
        $array['sign'] = self::getSign($array,$apiKey);
        $arrayToXml = self::curlPost('https://api.mch.weixin.qq.com/pay/unifiedorder',self::arrayToXml($array));
		//禁止引用外部xml实体
		libxml_disable_entity_loader(true);
        $simplexml = simplexml_load_string($arrayToXml, 'SimpleXMLElement', LIBXML_NOCDATA);
		$data = json_decode(json_encode($simplexml),true);
		switch ($data['return_code']){
			case "SUCCESS": 
			    $param = array('code'=>200,'url' =>$data['code_url']);
			break;			
			default: $param = array('code'=>201,'msg' =>$data['return_msg']);
		}
		return $param;
    }
	
	/**
     * 订单查询
     * @$appid 公众账号ID
	 * @$mch_id 商户号	
	 * @$transaction_id 微信订单号	
	 * @$out_trade_no 商户订单号
	 * @$sign API密钥
    */   
	public static function Query($appid,$mch_id,$transaction_id,$out_trade_no,$sign){        
        $array = array(
            'appid' =>$appid,
			'mch_id' =>$mch_id,
			'transaction_id' =>$transaction_id,
			'out_trade_no' =>$out_trade_no,
			'nonce_str' =>self::createNonceStr()
		);
		$array['sign'] = self::getSign($array,$sign);
        $arrayToXml = self::curlPost('https://api.mch.weixin.qq.com/pay/orderquery',self::arrayToXml($array));
        $simplexml = simplexml_load_string($arrayToXml, 'SimpleXMLElement', LIBXML_NOCDATA);
		$data = json_decode(json_encode($simplexml),true);
		switch ($data['trade_state']){
			case "SUCCESS": 
			    $param = array('code'=>200,'msg' =>'支付成功','orderid' =>$data['out_trade_no']);
			break;
			case "NOTPAY": 
			    $param = array('code'=>201,'msg' =>'未支付');
			break;
			case "CLOSED": 
			    $param = array('code'=>202,'msg' =>'已关闭');
			break;
			case "USERPAYING": 
			    $param = array('code'=>203,'msg' =>'支付中');
			break;
			case "PAYERROR": 
			    $Close = self::Close($appid,$mch_id,$out_trade_no,$sign);
				$param = array('code'=>204,'msg' =>'支付失败','text' =>$Close['msg']);
			break;
			case "ACCEPT": 
			    $param = array('code'=>205,'msg' =>'等待扣款');
			break;
			default: die('{"code": 206,"msg": "未知状态"}');
		}
		return $param;
    }
	
	/**
     * 关闭订单
     * @$appid 公众账号ID
	 * @$mch_id 商户号	
	 * @$out_trade_no 商户订单号
	 * @$sign API密钥
    */   
	public static function Close($appid,$mch_id,$out_trade_no,$sign){        
        $array = array(
            'appid' =>$appid,
			'mch_id' =>$mch_id,
			'out_trade_no' =>$out_trade_no,
			'nonce_str' =>self::createNonceStr()
		);
		$array['sign'] = self::getSign($array,$sign);
        $arrayToXml = self::curlPost('https://api.mch.weixin.qq.com/pay/closeorder',self::arrayToXml($array));
        $simplexml = simplexml_load_string($arrayToXml, 'SimpleXMLElement', LIBXML_NOCDATA);
		$data = json_decode(json_encode($simplexml),true);
		switch ($data['result_code']){
			case "SUCCESS": 
			    $param = array('code'=>200,'msg' =>'订单已关闭');
			break;			
			default: $param = array('code'=>201,'msg' =>'未知状态');
		}
		return $param;
    }
	//Post 提交
    public static function curlPost($url,$postData){
        if (is_array($postData)) {
            $postData = http_build_query($postData);
        }
		$ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$postData);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数       
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
	//16位随机字符串
    public static function createNonceStr($length = 16){
        $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $str = '';
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }
	//提交参数xml类型
    public static function arrayToXml($arr){
        $xml = "<xml>";
        foreach ($arr as $key => $val) {
            if (is_numeric($val)) {
                $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
            } else{
                $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
			}	
        }
        $xml .= "</xml>";
        return $xml;
    }
    /**
     * 获取签名
     */
    public static function getSign($params, $key){
        ksort($params, SORT_STRING);
        $unSignParaString = self::formatQueryParaMap($params, false);
        $signStr = strtoupper(md5($unSignParaString . "&key=" . $key));
        return $signStr;
    }
    protected static function formatQueryParaMap($paraMap, $urlEncode = false){
        $buff = "";
        ksort($paraMap);
        foreach ($paraMap as $k => $v) {
            if (null != $v && "null" != $v) {
                if ($urlEncode) {
                    $v = urlencode($v);
                }
                $buff .= $k . "=" . $v . "&";
            }
        }
        $reqPar = '';
        if (strlen($buff) > 0) {
            $reqPar = substr($buff, 0, strlen($buff) - 1);
        }
        return $reqPar;
    }
}